import { BASE_URL } from '@/config';
import { request } from '@/utils/request';

/**
 * 用户认证相关接口
 */

// 登录接口
export interface LoginParams {
  phoneNumber: string;
  passwordHash: string;
}

export interface LoginResponse {
  token: string;
}

export function login(data: LoginParams) {
  return request<LoginResponse>({
    url: `${BASE_URL}/user/login`,
    method: 'POST',
    data
  });
}

// 注册接口
export interface RegisterParams {
  phoneNumber: string;
  password: string;
  confirmPassword: string;
  code: string;
  nickname?: string;
  avatarUrl?: string;
  gender?: string;
}

export function register(data: RegisterParams) {
  return request({
    url: `${BASE_URL}/user/register`,
    method: 'POST',
    data
  });
}

// 发送验证码
export function sendSmsCode(phoneNumber: string) {
  return request({
    url: `${BASE_URL}/sms/send/message/${phoneNumber}`,
    method: 'POST'
  });
}

//退出登录
export function logout() {
  return request({
    url: `${BASE_URL}/user/logout`,
    method: 'POST'
  });
}

//根据手机获取用户id
export function getCustomerIdByPhone(phoneNumber: string) {
  return request<number>({
    url: `${BASE_URL}/user/getCustomerIdByPhone`,
    method: 'GET',
    params: { phoneNumber }
  });
}

// 获取用户个人资料
export function getUserProfile(customerId: number) {
  return request({
    url: `${BASE_URL}/user/users/profile/${customerId}`,
    method: 'GET'
  });
}

// 更新用户个人资料
export function updateUserProfile(customerId: number, data: any) {
  return request({
    url: `${BASE_URL}/user/users/profile/${customerId}`,
    method: 'PUT',
    data
  });
}

// 获取用户所有地址
export function getUserAddresses(customerId: number) {
  return request({
    url: `${BASE_URL}/user/addresses/customer/${customerId}`,
    method: 'GET'
  });
}

// 获取地址详情
export function getAddressDetail(customerId: number, addressId: number) {
  return request({
    url: `${BASE_URL}/user/addresses/customer/${customerId}/address/${addressId}`,
    method: 'GET'
  });
}

// 新增地址
export function addAddress(data: any) {
  return request({
    url: `${BASE_URL}/user/addresses`,
    method: 'POST',
    data
  });
}

// 修改地址
export function updateAddress(customerId: number, addressId: number, data: any) {
  return request({
    url: `${BASE_URL}/user/addresses/customer/${customerId}/address/${addressId}`,
    method: 'PUT',
    data
  });
}

// 删除地址
export function deleteAddress(customerId: number, addressId: number) {
  return request({
    url: `${BASE_URL}/user/addresses/customer/${customerId}/address/${addressId}`,
    method: 'DELETE'
  });
}

// 获取用户收藏列表
export function getUserFavorites(customerId: number) {
  return request({
    url: `${BASE_URL}/user/favorites/customer/${customerId}`,
    method: 'GET'
  });
}

// 删除收藏
export function deleteFavorite(customerId: number, productId: number) {
  return request({
    url: `${BASE_URL}/user/favorites/customer/${customerId}/product/${productId}`,
    method: 'DELETE'
  });
}

// 获取用户钱包
export function getUserWallet(customerId: number) {
  return request({
    url: `${BASE_URL}/user/account/customer/${customerId}`,
    method: 'GET'
  });
}

// 获取钱包流水
export function getWalletLog(customerId: number, walletId: number) {
  return request({
    url: `${BASE_URL}/user/log/customer/${customerId}/wallet/${walletId}`,
    method: 'GET'
  });
}

// 根据区域查询愿望榜单
export function getWishlistByRegion(regionId: number) {
  return request({
    url: `${BASE_URL}/user/wishlists/region/${regionId}`,
    method: 'GET'
  });
}

// 添加愿望
export function addWishlist(regionId: number, customerId: number, data: any) {
  return request({
    url: `${BASE_URL}/user/wishlists/add/${regionId}/${customerId}`,
    method: 'POST',
    data
  });
}